<?php
require_once '../Controller/ConnectFB.php';
require '../Controller/Config.php';

@session_regenerate_id();        
$ses_id = session_id(); 
$_SESSION['ACCESS_PAGE_ID'] = $ses_id;

$yearnow = date("Y");
$monthnow = date("n");
$daynow = date("j");

$dateuser = explode("/", $fb_userData['birthday']);
if($dateuser[0]!=null && $dateuser[1]!=null && $dateuser[2]!=null){
    $caldatenow = mktime(0, 0, 0, $monthnow, $daynow, $yearnow);
    $caldateuser = mktime(0, 0, 0, $dateuser[0], $dateuser[1], $dateuser[2]);
    $agesec = $caldatenow-$caldateuser;
    $age = $agesec/31556736;
}
$location_tmp = explode(", ", $fb_userData['location']['name']);
$location = null;
$sex = $fb_userData['gender'];
if($sex=="male"){
    $sex="Male";
}else if($sex=="female"){
    $sex="Female";
}else{
    $sex=0;
}
$cnt = count($location_tmp);

//    echo $cnt . "<br />";    
if($fb_userData['location']['name']==NULL){
//    echo "Location is null" . "<br />";
}else{
    if($cnt==1){
//        echo $location_tmp[0] . "<br />";
        $location = $location_tmp[0];
    }else if($cnt==2){
//        echo $location_tmp[0] . "<br />";
        $location = $location_tmp[0];
    }else if($cnt==3){
//        echo $location_tmp[1] . "<br />";
        $location = $location_tmp[1];
    }
}
?>
    <link rel="stylesheet" href="../jquery/css/start/jquery-ui-1.10.1.custom.css" type="text/css" />
        
    <script>
        function checkFrm(){
            var occpu = document.getElementById('occpu').value;
            var country = document.getElementById('country').value;
            var sex = document.getElementById('sex').value;
            var age = document.getElementById('age').value;

            var pollId = '<?php echo $_GET['pollId'] ?>';
            var poll = '<?php echo $_GET['poll'] ?>';

            if(occpu==0 || country==0 || sex==0 || age==0){
                alert("โปรดกรอกข้อมูลให้ครบ !!!");
                return false;
            }else{
                //alert(encodeURI("../View/viewPoll.php?pollId="+pollId+"&poll="+poll+""));
                //window.location.href = encodeURI("../View/viewPoll.php?pollId="+pollId+"&poll="+poll);
                return true;
            }
        }
        
        $(function() {
            $( "#btnNext" )
              .button()
              .click(function( event ) {
//                event.preventDefault();           
              });
        });
        
    </script>

    <link rel="stylesheet" href="../jquery/css/start/jquery-ui-1.10.1.custom.css" type="text/css" />
    
<table width="100%" border="0" align="center">
                            <tr valign="top" height="40px">
                                <td colspan="5" border="1" align="center" valign="middle" bgcolor="#e96c10">
                                    <font color="#ffffff" size="4" align="center"><b>หัวข้อแบบสอบถาม : <?php echo $_GET['poll']?></b></font>
                                </td>
                            </tr>
                            <tr>
                                <td align="center">
                                    <font size="4" align="left">กรุณากรอกข้อมูลส่วนด้านล่างนี้ให้ครบถ้วน</font>
                                </td>
                            </tr>
                    </table>

<!doctype html>
<form onsubmit="return checkFrm()" id="formMain" name="formMain" method="POST" action="../Controller/CheckConditionProcess.php" >
      <FIELDSET class="fieldset ui-widget-content ui-corner-all"><LEGEND>ข้อมูลผู้ใช้งาน/User Info</LEGEND>
                <label for="occupation">สายอาชีพของท่าน/Occupation</label><br />
                <select id="occpu" name="occpu" style="width:90%"  class="" />
                <option value="0" selected="selected">No data/ไม่มีข้อมูล</option>
                <?php
                    $checklastid = 0;
                    $q1="select * from m_occupation order by occupation_name_eng";
                    $sql2=mysql_query($q1);
                    while($rs=mysql_fetch_array($sql2)){
                ?>
                <option value="<?php echo $rs['occupation_id']?>"><?php echo $rs['occupation_name_eng']." - ".$rs['occupation_name']?></option>
                <?php   
                        if($checklastid<$rs['occupation_id']){
                            $checklastid = $rs['occupation_id']; 
                        }
                    }
                     $lastid = $checklastid + 1;
                ?>   
                <option value="<?php echo $lastid?>">Others - อื่นๆ</option>
                </select>

                <br /><br />

                <label for="county">จังหวัดที่อยู่ปัจจุบันของท่าน/Country</label><br />
                <select id="country" name="country" style="width:90%" class="">
                <option value="0" selected="selected">No data/ไม่มีข้อมูล</option>
                <?php
                    $q="select * from m_province order by province_name_eng";
                    $qr=mysql_query($q);
                    while($rs=mysql_fetch_array($qr)){
                ?>
                <option value="<?php echo $rs['province_id']?>"<?php  if($location == $rs['province_name_eng']) echo 'selected';?>><?php echo $rs['province_name_eng']." - ".$rs['province_name']?></option>
                <?php 
                
                    }
                   
                ?>
                </select>

                <br /><br />
                
                <label for="sex">เพศของท่าน/Sex</label><br />
                <select name="sex" id="sex" size="1" style="width:90%">
                <option value="0" selected="selected">No data/ไม่มีข้อมูล</option>
                <?php
                    $q="SELECT * FROM m_sex ORDER BY sex_id";
                    $qr=mysql_query($q);
                    while($rs=mysql_fetch_array($qr)){
                
                    if($rs['sex_id']==0){
                ?>
                    <option value="<?php echo $rs['sex_id']?>"<?php  if($sex == $rs['sex_eng']) echo 'selected';?>><?php echo $rs['sex_eng']." / ".$rs['sex_thai']?></option>
                <?php    }else{ ?>
                    <option value="<?php echo $rs['sex_id']?>"<?php  if($sex == $rs['sex_eng']) echo 'selected';?>><?php echo $rs['sex_eng']." / ".$rs['sex_thai']?></option>
                <?php 
                        }
                 }
                ?>
                </select>

                <br /><br />
                <label for="age">ช่วงอายุของท่าน/Age Range</label><br />
                <select name="age" id="age" size="1" style="width:90%">
                <option value="0" selected="selected">No data/ไม่มีข้อมูล</option>
                <?php
                    $q="SELECT * FROM m_age_range ORDER BY age_range_id";
                    $qr=mysql_query($q);
                    while($rs=mysql_fetch_array($qr)){
                ?>
                <option value="<?php echo $rs['age_range_id']?>"<?php  if(floor($age)>=$rs['age_range_from']&&floor($age)<=$rs['age_range_to']) echo 'selected'; ?>><?php echo $rs['age_range_from']." - ".$rs['age_range_to']?> Year old</option>
                <?php 
                
                    }
                    mysql_close();
                ?>
                </select>
                
                <br /><br />
                <input type="hidden" name="pollId" value="<?php echo $_GET['pollId'] ?>" />
                <input type="hidden" name="poll" value="<?php echo $_GET['poll'] ?>" />
                <button type="submit" name="btn" id="btnNext" >Next</button>
            </FIELDSET>
    </form>



